
#include <SDL/SDL_video.h>

#include "devices/mouse.h"
#include "devices/mouse_private.h"

#include "system/window.h"

void _esdlMouseInit(EsdlMouse* mouse) {
    mouse->x = 0;
    mouse->y = 0;

    mouse->show = true;
    mouse->glue = false;
    
    for (unsigned i = 0; i < 8; i++) {
        mouse->buttons[i] = false;
    }
}

void _esdlMouseSetButtonState(EsdlMouse* mouse, EsdlMouseButton button, bool pressed) {
    mouse->buttons[button] = pressed;
}

bool _esdlMouseIsButtonPressed(const EsdlMouse* mouse, EsdlMouseButton button) {
    return mouse->buttons[button];
}

bool esdlIsMouseButtonPressed(EsdlMouseButton button) {
    return _esdlMouseIsButtonPressed(esdlGetMouse(), button);
}

void mouseShow() {
    esdlGetMouse()->show = true;
    SDL_ShowCursor(SDL_ENABLE);
}

void mouseHide() {
    esdlGetMouse()->show = false;
    SDL_WarpMouse(esdlGetMouse()->x, esdlGetMouse()->y);
    SDL_ShowCursor(SDL_DISABLE);
}

void esdlGlueMouse(bool glue) {
    esdlGetMouse()->glue = glue;
    SDL_WM_GrabInput(glue?SDL_GRAB_ON:SDL_GRAB_OFF);
}
